package com.example.dialog.Adapter;

import static android.content.ContentValues.TAG;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.dialog.Activity.ArticleDetailsActivity;
import com.example.dialog.R;
import com.example.dialog.Tools.MyImageView;
import com.example.dialog.model.Post;

import java.util.ArrayList;
import java.util.List;

public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder> {

    private List<Post> posts = new ArrayList<>();

    // 设置数据源
    @SuppressLint("NotifyDataSetChanged")
    public void setPosts(List<Post> posts) {
        this.posts = posts;
        notifyDataSetChanged();  // 数据源更新时，通知适配器刷新
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 使用 LayoutInflater 来加载视图
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_post, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Post post = posts.get(position);
        Log.d(TAG, "Like Count: " + post.getMtv_data_like());  // 确认点赞数是否正确
        // 设置视图中的数据
        holder.imageResourceId.setImageURL(post.getImageResourceId());
        Log.e(TAG, "onBindViewHolder:" + post.getImageResourceId());
        holder.title.setText(post.getTitle());
        holder.avatarResourceId.setImageURL(post.getAvatarResourceId());
        holder.name.setText(post.getName());
        holder.likeCount.setText(post.getMtv_data_like());

        // 设置点击事件，跳转到 ArticleDetailsActivity
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 获取当前Post的数据
                Intent intent = new Intent(view.getContext(), ArticleDetailsActivity.class);
                // 将Post的所有数据传递给ArticleDetailsActivity
                intent.putExtra("mtv_title", post.getTitle());
                intent.putExtra("usrImage", post.getAvatarResourceId());
                intent.putExtra("mtv_author", post.getName());
                intent.putExtra("mtv_summary", post.getMtv_content());
                intent.putExtra("contentImage", post.getContentImage());
                intent.putExtra("mtv_content", post.getMtv_content());


                intent.putExtra("mtv_data_read", post.getMtv_data_read());  // 假设阅读量为100

                intent.putExtra("mtv_data_like", post.getMtv_data_like());
                intent.putExtra("mtv_data_star", post.getMtv_data_star());  // 假设收藏数为50
                view.getContext().startActivity(intent);
            }
        });
    }


    @Override
    public int getItemCount() {
        return posts.size();
    }

    // ViewHolder类
    public static class ViewHolder extends RecyclerView.ViewHolder {
        MyImageView imageResourceId;
        TextView title;
        MyImageView avatarResourceId;
        TextView name;
        TextView likeCount;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            // 初始化视图控件
            imageResourceId = itemView.findViewById(R.id.imageView);
            title = itemView.findViewById(R.id.titleText);
            avatarResourceId = itemView.findViewById(R.id.avatarView);
            name = itemView.findViewById(R.id.nameText);
            likeCount = itemView.findViewById(R.id.tv_like_cnts);
        }
    }
}
